# -*- coding: utf-8 -*-

from odoo import api, fields, models, tools

class SaleSubmit(models.Model):
    _inherit = ["sale.order"]

    is_submit = fields.Boolean('Is Submit', default=False)

    @api.multi
    def sale_submit_button(self):
        self.write({
            'is_submit': True
        })

        # res = super(SaleSubmit, self).sale_submit_button()
        # for button in self:
        #     print button
        #     for a in button:
        #         a.state='sent'
        return self.write({'state': 'sent'})
        # return res


    @api.multi
    def action_confirm(self):
        self.write({
            'is_submit':True
        })
        for order in self:
            order.state = 'sale'
            order.confirmation_date = fields.Datetime.now()
            if self.env.context.get('send_email'):
                self.force_quotation_send()
            order.order_line._action_procurement_create()
        if self.env['ir.values'].get_default('sale.config.settings', 'auto_done_setting'):
            self.action_done()
        return True

        # orders = self.filtered(lambda s: s.state in ['cancel', 'sent'])
        # orders.write({
        #     'state': 'draft',
        #     'procurement_group_id': False,
        # })
        # return orders.mapped('order_line').mapped('procurement_ids').write({'sale_line_id': False})
    # @api.multi
    # def change_and_copy(self):
    #     self.ensure_one()
    #     if self.is_copy:
    #         raise Warning(u'此单已经为复制订单,请使用原始订单进行复制')
    #     for sl in self.order_line:
    #         sl.origin_id = sl.id
    #     new_order = self.copy()
    #     new_order.requested_date = self.requested_date
    #     new_order.source_order_id = self
    #     new_order.order_line_clone = [(5, 0)]
    #     for line in self.order_line:
    #         tax = None
    #         if len(line.tax_id) > 0:
    #             tax = line.tax_id[0]
    #         new_order.order_line_clone = [(0, 0, {
    #             'origin_id': line.id,
    #             'currency_id': line.currency_id,
    #             'customer_lead': line.customer_lead,
    #             'customer_order': line.customer_order,
    #             # 'date_order': line.date_order,
    #             # 'datetime_order': line.datetime_order,
    #             'discount': line.discount,
    #             # 'incoterm': line.incoterm,
    #             'length_weight': line.length_weight,
    #             'lot': line.lot,
    #             # 'margin': line.margin,
    #             'name': line.name,
    #             'note': line.note,
    #             'order_id': line.order_id,
    #             'order_partner_id': line.order_partner_id,
    #             'price_reduce': line.price_reduce,
    #             'price_reduce_taxexcl': line.price_reduce_taxexcl,
    #             'price_reduce_taxinc': line.price_reduce_taxinc,
    #             'price_subtotal': line.price_subtotal,
    #             'price_tax': line.price_tax,
    #             'price_total': line.price_total,
    #             'price_unit': line.price_unit,
    #             'contra': line.product_id,
    #             'product_packaging': line.product_packaging,
    #             'product_tmpl_id': line.product_tmpl_id,
    #             'product_uom': line.product_uom,
    #             'product_uom_qty': line.product_uom_qty,
    #             # 'purchase_price': line.purchase_price,
    #             # 'route_id': line.route_id,
    #             # 'salesman_id': line.salesman_id,
    #             'sequence': line.sequence,
    #             'tax_id': tax,
    #             'state': 'draft',
    #             'technological_requirements': line.technological_requirements
    #         })]
    #     new_order.is_copy = True
    #     new_order.client_order_ref = self.client_order_ref
    #     # new_order.approve_reason_ids = [(5, 0)]
    #     if self.state in ['draft', 'sent', 'sale']:
    #         self.write({'state': 'cancel'})
    #     if len(self.approve_reason_ids) > 1:
    #         try:
    #             new_order.action_confirm()
    #         except:
    #             new_order.action_confirm_with_credit()
    #     return new_order